<template>
  <div class="statistics-container">
    <div class="header">
      <h1>数据统计</h1>
    </div>
    
    <div class="content">
      <div class="stats-grid">
        <div class="stat-card">
          <h3>本月订单</h3>
          <p class="stat-value">{{ stats.monthlyOrders || 0 }}</p>
          <p class="stat-change">本月累计</p>
        </div>
        
        <div class="stat-card">
          <h3>本月收入</h3>
          <p class="stat-value">¥{{ formatMoney(stats.monthlyRevenue) }}</p>
          <p class="stat-change">本月累计</p>
        </div>
        
        <div class="stat-card">
          <h3>平均订单额</h3>
          <p class="stat-value">¥{{ formatMoney(stats.avgOrderValue) }}</p>
          <p class="stat-change">本月平均</p>
        </div>
        
        <div class="stat-card">
          <h3>客户满意度</h3>
          <p class="stat-value">{{ stats.satisfaction }}%</p>
          <p class="stat-change">↑ 3% 比上月</p>
        </div>
      </div>
      
      <div class="chart-section">
        <h2>最近7天订单趋势</h2>
        <div class="chart-placeholder">
          <p>图表功能开发中...</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Statistics',
  data() {
    return {
      stats: {
        monthlyOrders: 34,
        monthlyRevenue: '22231.00',
        avgOrderValue: '12.00',
        customerCount: 121,
        repeatCustomers: 14,
        satisfaction: 5
      },
      loading: false
    }
  },
  mounted() {
    this.loadStatistics()
  },
  methods: {
    async loadStatistics() {
      try {
        const businessId = this.$store.state.businessId
        if (!businessId) {
          this.$router.push('/login')
          return
        }

        this.loading = true
        const response = await this.$axios.post('/elm/BusinessManagementController/getMonthlyStats', null, {
          params: { businessId }
        })

        if (response.data && (response.data.code === 200 || response.data.code === '200')) {
          const result = response.data.result
          this.stats = {
            monthlyOrders: result.monthlyOrders || 0,
            monthlyRevenue: result.monthlyRevenue || '0.00',
            avgOrderValue: result.avgOrderValue || '0.00',
            customerCount: result.customerCount || 0,
            repeatCustomers: result.repeatCustomers || 0,
            satisfaction: result.satisfaction || 0
          }
          console.log('统计数据加载成功:', this.stats)
        } else {
          console.error('获取统计数据失败:', response.data?.message)
        }
      } catch (error) {
        console.error('加载统计数据失败:', error)
        alert('加载统计数据失败，请重试')
      } finally {
        this.loading = false
      }
    },

    // 格式化金额显示
    formatMoney(amount) {
      if (!amount) return '0.00'
      const num = parseFloat(amount)
      return num.toFixed(2)
    }
  }
}
</script>

<style scoped>
.statistics-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #f5f5f5;
}

.header {
  background: white;
  padding: 20px 30px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.header h1 {
  margin: 0;
  font-size: 24px;
  color: #333;
}

.content {
  flex: 1;
  overflow-y: auto;
  padding: 30px;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 20px;
  margin-bottom: 30px;
}

.stat-card {
  background: white;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.stat-card h3 {
  margin: 0 0 10px 0;
  color: #666;
  font-size: 14px;
}

.stat-value {
  margin: 0 0 5px 0;
  font-size: 28px;
  font-weight: 600;
  color: #333;
}

.stat-change {
  margin: 0;
  font-size: 12px;
  color: #4caf50;
}

.chart-section {
  background: white;
  border-radius: 10px;
  padding: 20px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

.chart-section h2 {
  margin: 0 0 15px 0;
  font-size: 16px;
  color: #333;
}

.chart-placeholder {
  height: 300px;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f9f9f9;
  border-radius: 8px;
  color: #999;
}
</style>
